package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultPage;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.AllocationWarehouseOutMapper;
import com.ant.storage.pojo.AllocationAndCommodity;
import com.ant.storage.pojo.AllocationWarehouseOut;
import com.ant.storage.service.IAllocationWarehouseOutService;
import com.ant.storage.service.ICityService;
import com.ant.storage.service.IInventoryService;
import com.ant.storage.utils.NullInspectUtil;
import com.ant.storage.common.PageRecv;
import com.ant.storage.common.ResultVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AllocationWarehouseOutServiceImpl extends ServiceImpl<AllocationWarehouseOutMapper,AllocationWarehouseOut> implements IAllocationWarehouseOutService{

    @Autowired(required = false)
    AllocationWarehouseOutMapper allocationWarehouseOutMapper;

    @Autowired
    private ICityService cityService;

    @Autowired
    IInventoryService inventoryService;

    @Override
    public ResultPage queryAllocationWarehouseOut(PageRecv<AllocationWarehouseOut> pageRecv) {
        Map map = new HashMap<>();
        if (!(pageRecv.getData()==null)){
            map = NullInspectUtil.ClassChangeMapNotNull(pageRecv.getData());
        }
        Integer page = (pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        Integer limit = pageRecv.getCurrentPage()*pageRecv.getPageSize();
        List<AllocationWarehouseOut>list = allocationWarehouseOutMapper.queryAllocationWarehouseOut(page,limit,map);
        for (AllocationWarehouseOut allocationWarehouseOut:list) {
            allocationWarehouseOut.getAllocation().getSupply_city().setName(cityService.idQuery(allocationWarehouseOut.getAllocation().getAllocation_supply_city_id()));
            allocationWarehouseOut.getAllocation().getSupply_dept().setName(cityService.idQuery(allocationWarehouseOut.getAllocation().getAllocation_supply_dept_id()));
            allocationWarehouseOut.getAllocation().getExtract_city().setName(cityService.idQuery(allocationWarehouseOut.getAllocation().getAllocation_extract_city_id()));
            allocationWarehouseOut.getAllocation().getExtract_dept().setName(cityService.idQuery(allocationWarehouseOut.getAllocation().getAllocation_extract_dept_id()));
        }
        Integer count = allocationWarehouseOutMapper.countAllocationWarehouseOut(map);
        return ResultPage.success("查询成功",list,count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo allocationWarehouseOutEdit(AllocationWarehouseOut allocationWarehouseOut,Integer type) {
        for(AllocationAndCommodity allocationAndCommodity:allocationWarehouseOut.getAllocation().getAllocationAndCommodity()){
            if(allocationAndCommodity.getCommodity_enter_quantity() == null){
                continue;
            }
            if (0>allocationAndCommodity.getCommodity_out_quantity() || allocationAndCommodity.getCommodity_out_quantity()>allocationAndCommodity.getCommodity_allocation_quantity()){
                throw new AntException(ResultCode.ALLOCATION_OUT_NUMBER);
            }
            if(allocationAndCommodity.getOut_warehouse().getId()<=0 ||allocationAndCommodity.getOut_warehouse().getId() == null){
                throw new AntException(ResultCode.WAREHOUSE_NULL);
            }
            if(!inventoryService.commodityInventoryJudge(allocationAndCommodity.getCommodity().getCommodityId(),allocationAndCommodity.getOut_warehouse().getId(),allocationAndCommodity.getCommodity_out_quantity())){
                throw new AntException(20001,allocationAndCommodity.getCommodity().getCommodityName()+"库存不足");
            }
            allocationWarehouseOutMapper.updateAllocationAndCommodity(allocationWarehouseOut.getAllocation().getAllocation_id(),allocationAndCommodity.getCommodity().getCommodityId(),allocationAndCommodity.getCommodity_out_quantity());

        }
        allocationWarehouseOut.setAllocation_warehouse_out_type(type);
        UpdateWrapper<AllocationWarehouseOut> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("allocation_warehouse_out_id",allocationWarehouseOut.getAllocation_warehouse_out_id());
        this.update(allocationWarehouseOut,updateWrapper);
        return ResultVo.success("ok");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stockSubtract(AllocationWarehouseOut allocationWarehouseOut) {
        for(AllocationAndCommodity allocationAndCommodity:allocationWarehouseOut.getAllocation().getAllocationAndCommodity()){
            inventoryService.stockSubtract(allocationAndCommodity.getOut_warehouse_id(),allocationAndCommodity.getCommodity().getCommodityId(),allocationAndCommodity.getCommodity_out_quantity());
        }
    }
}
